package com.yunxun.coinmessage.service;

import com.yunxun.coinmessage.dao.ProgramDao;

import com.yunxun.coinmessage.entity.Program;
import com.yunxun.coinmessage.templatedata.MsgResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProgramService {
    @Autowired
    ProgramDao programDao;
    MsgResult msgResult;


        public MsgResult add(Program program){
            Program pro=programDao.queryByName(program.getProgram_name());
            //判断是否有同名栏目
            if(pro!=null){
                msgResult=new MsgResult(false,"添加失败，请勿输入相同的栏目名");
                return msgResult;
            }
            int tag= programDao.add(program.getProgram_name());

            //判断是否添加成功
            if(tag>0){
                msgResult=new MsgResult(true,"添加成功");
                return msgResult;
            }else {
                msgResult=new MsgResult(false,"添加失败，请稍后尝试");
                return msgResult;
            }

        }


        public Integer queryTotal(){
             return  programDao.queryTotal();
        }

        public MsgResult updateById(Program program){
          int tag= programDao.updateById(program);
            if(tag>0){
                msgResult=new MsgResult(true,"修改成功");
            }else{
                msgResult=new MsgResult(false,"修改失败");
            }
            return msgResult;
        }

        public MsgResult deleteById(Integer program_id){
            int tag= programDao.deleteById(program_id);
            if (tag>0){
                msgResult=new MsgResult(true,"删除成功");
            }else{
                msgResult=new MsgResult(false,"删除失败");
            }
            return msgResult;
        }

        public List<Program> queryAll(){
            return programDao.queryAll();
        }
        public Program queryById(Integer program_id){
            return  programDao.queryById(program_id);
        }
        public  Program queryByName(String program_name){
            return  programDao.queryByName(program_name);
        }
        public String queryNameById(int program_id){
            return programDao.queryNameById(program_id);
        }
}
